<template>
	<view class="cart-container" v-if="JSON.stringify(cart)!=='[]'">
		<!-- 收获地址组件 -->
		<my-address></my-address>
		<!-- 商品列表标题 -->
		<view class="cart-title">
			<!-- 左侧图标 -->
			<uni-icons type="shop" size="18"></uni-icons>
			<!-- 右侧文本 -->
			<text class="cart-title-text">购物车</text>
		</view>
		<!-- 滑动删除 -->
		<uni-swipe-action ref="swipeAction">
			<!-- 商品列表 -->
			<block v-for="(item,index) in cart" :key="index">
				<uni-swipe-action-item :right-options="options" @click="swipeClick(item)">
					<my-goods :item="item" :showRadio="true" :showNum="true" @radio-change="radioChangeHandle" @num-Change="numChangeHandle"></my-goods>
				</uni-swipe-action-item>
			</block>
		</uni-swipe-action>
		<!-- 自定义结算组件 -->
		<my-settle></my-settle>
	</view>
	
	<!-- 空白购物车区域 -->
	<view class="empty-cart" v-else>
		<image src="@/static/cart_empty@2x.png" class="empty-image"></image>
		<text class="empty-text">空空如也</text>
	</view>
</template>

<script>
	// 导入maxins--自定义tabBar购物车右上角数字徽标组件
	import badgeMix from '@/mixins/maxins.js'
	import {mapState,mapMutations} from 'vuex'
	export default {
		// 使用自定义maxins
		mixins:[badgeMix],
		
		computed:{
			...mapState('my_cart',['cart'])
		},
		methods:{
			...mapMutations('my_cart',['updateCartListStateData','updateCartListCountData','deleteCartListData']),
			
			// 点击组件改变购物车选中状态
			radioChangeHandle(value){
				// 调用mutations修改购物车选中状态
				this.updateCartListStateData(value)
			},
			
			// 数字框变化
			numChangeHandle(newValue){
				this.updateCartListCountData(newValue)
			},
			// 商品右侧滑动
			swipeClick(goods){
				this.deleteCartListData(goods)
				this.setBadge()		
				this.$refs.swipeAction.closeAll();
			},
		},
		data() {
			return {
				options:[{
					text: '删除',
					style: {
						backgroundColor: '#C00000'
					}
				}]
			};
		}
	}
</script>

<style lang="scss">
	.cart-container{
		padding-bottom: 50px;
	}
	.cart-title{
		height: 40px;
		display: flex;
		align-items: center;
		padding-left: 5px;
		border-bottom: 1px solid #efefef;
		.cart-title-text{
			font-style: 14px;
			margin-left: 10px;
		}
	}
	.empty-cart{
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 150px;
		.empty-image{
			width: 90px;
			height: 90px;
		}
		.empty-text{
			font-style: 12px;
			color: gray;
			margin-top: 15px;
		}
	}
</style>
